package auth;

import io.*;

import java.sql.*;

import org.eclipse.swt.widgets.Text;

import track.Album;
import track.Artist;
import track.Genre;
import track.Song;
import track.playlistEntry;

/**
 * This is a static library that holds various functions that involve users
 * @author Alex
 *
 */
public class UserFuncs {

	/**
	 * Register a new user to the database
	 * 
	 * @param username
	 * @param email
	 * @param name
	 * @param password
	 * 
	 * @post The registration checks that the username is free, so there is no real need to check it
	 * 
	 * @return was the registration successful?
	 */
	public static boolean registerUser(String username, String email, String name, String password)
	{
		if(isFreeUsername(username)) {
			SqlDb db = new SqlDb();
			Connection conn = db.getConnection();
			
			try {
				Statement stmt = conn.createStatement();
				stmt.execute("INSERT INTO users(username, name, email, password) VALUES" +
						"('" + username + "','" + name + "','" + email + "','" + password + "')");
				
				stmt.close();
				return true;
			}
			catch(SQLException e) {
				return false;
			}
		}
		else
			return false;
	}
	
	/**
	 * Check if the username is taken
	 * 
	 * @param username - The searched username as a string
	 * 
	 * @return is the username taken?
	 */
	public static boolean isTakenUsername(String username)
	{
		SqlDb db = new SqlDb();
		Statement stmt;
		ResultSet result;
		Connection conn = db.getConnection();
		
		try {
			//count the number of the strings that are that username. If there are none, the username is free.
			stmt = conn.createStatement();
			result = stmt.executeQuery("SELECT COUNT(*) " +
					     			   "FROM users" +
					     			   "WHERE username=" + username);
			
			return !(result.getInt(1) == 0);
		}
		catch(SQLException e) {
			return false;
		}
	}
	
	/**
	 * Check if the username is free
	 * 
	 * @param username - The searched username
	 * 
	 * @return is the username free?
	 */
	public static boolean isFreeUsername(String username)
	{
		return (!isTakenUsername(username));
	}

	/**
	 * Returns the user based on it's username and password
	 *  
	 * @param username
	 * @param password
	 * 
	 * @post returns null if the user doesn't exist
	 * 
	 * @return User
	 */
	public static User getUser(String username, String password) 
	{
		Connection conn = SqlDb.getConnection();
		Statement stmt;
		ResultSet result;
		
		try {
			stmt = conn.createStatement();
			result = stmt.executeQuery("SELECT id " +
									   "FROM users " +
									   "WHERE username ='" + username + "' AND " +
									   		  "password='" + password + "'");
			if(result.isAfterLast()) { //Check if there are results
				return null;
			}
			else {
				result.next();
				
				User user = new User(result.getInt(1));
				return user;
			}
		} 
		catch(SQLException e) {
			//if an exception was thrown
			return null;
		}
	}

}
